/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ipercube.swing;

import com.ipercube.Column;
import com.ipercube.DataMap;
import com.ipercube.Measure;
import com.ipercube.Pivot;
import com.ipercube.Row;
import com.ipercube.model.AxisValue;
import com.ipercube.model.CollapsibleTree;
import com.ipercube.model.ColumnModel;
import com.ipercube.model.Node;
import com.ipercube.model.RowModel;
import com.ipercube.model.Tree;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 *
 * @author tartur
 */
public class PivotTableModel implements TableModel {

    private RowModel rowModel;
    private ColumnModel colModel;
    private DataMap dataMap;
    private Pivot pivot;


    private <T extends AxisValue> CollapsibleTree<T> buildCollapsible(Tree<T,?> tree){
        return null;
    }

    protected CollapsibleTree<Row> getRTree(){
        return buildCollapsible(dataMap.getRowsTree());
    }

    protected CollapsibleTree<Column> getCTree(){
        return buildCollapsible(dataMap.getColumnsTree());
    }

    protected Row getRowAt(int index){
        Row r = null;
        int depth = getRTree().getDepth();
        Node<Row,Boolean> n = getRTree().getNodeByIndexAt(depth, index);
        r = n.getNode();
        return r;
    }

    protected Column getColumnAt(int index){
        Column c = null;
        int depth = getCTree().getDepth();
        Node<Column,Boolean> n = getCTree().getNodeByIndexAt(depth, index);
        c = n.getNode();
        return c;
    }

    public int getRowCount() {
        int depth = getRTree().getDepth();
        return getRTree().getNodesCount(depth);
    }

    public int getColumnCount() {
        int depth = getCTree().getDepth();
        return getCTree().getNodesCount(depth);
    }

    public String getColumnName(int columnIndex) {
        String name = "";
        // if index < rowDepth => colName is rowDimension
        Column c = getColumnAt(columnIndex);
        return name;
    }

    public Class<?> getColumnClass(int columnIndex) {
        // if index < rowDepth => colClass is rowDimension type
        return pivot.getMeasureEvaluator().getMeasureType();
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Row row = getRowAt(rowIndex);
        Column column = getColumnAt(columnIndex);
        Measure m = pivot.getMeasureAt(row, column);
        return m.getValue();
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addTableModelListener(TableModelListener l) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void removeTableModelListener(TableModelListener l) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
